﻿using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;

namespace AutoLoL.Playground
{
    public class MemoryManager
    {

        //import kernel32 and create OpenProcess and ReadProcess functions
        [DllImport("kernel32.dll")]
        static extern IntPtr OpenProcess(UInt32 dwDesiredAccess, Boolean bInheritHandle, UInt32 dwProcessId);

        [DllImport("kernel32.dll")]
        public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,
        byte[] lpBuffer, UIntPtr nSize, uint lpNumberOfBytesWritten);

        //Create handle
        IntPtr Handle;
        
        //constructor
        public MemoryManager(string sprocess)
        {
            //Get the specific process
            
            Process[] Processes = Process.GetProcessesByName(sprocess);
            Process nProcess = Processes[0];
            //access to the process
            Handle = OpenProcess(0x0400, false, (uint)nProcess.Id);
        }

        //function ReadString (returns string value)
        public string ReadString(uint pointer)
        {
            //Create bytearray
            byte[] bytes = new byte[24];

            //Read the specific address within the process
            ReadProcessMemory(Handle, (IntPtr)pointer, bytes, (UIntPtr)24, 0);
            //Return the result as UTF8 String
            return Encoding.UTF8.GetString(bytes);
        }

        public uint GetPointerAddress(uint pointer,uint offset)
        {
           
            //Creating the address (reading the Base and add the offset)
            uint adress = (uint)ReadPointer(pointer) + offset;

            return adress;
        }

        //function ReadOffset (returns int value)
        public int ReadOffset(uint pointer, params uint[] offsets)
        {
            //Create bytearray
            byte[] bytes = new byte[24];

            uint adress = pointer;

            foreach (uint offset in offsets)
            {
                adress = GetPointerAddress(adress, offset);
            }

            //Reading the specific address within the process
            ReadProcessMemory(Handle, (IntPtr)adress, bytes, (UIntPtr)sizeof(int), 0);
            //Return the result as 4 byte int
            return BitConverter.ToInt32(bytes, 0);
        }

        //function ReadPointer (returns int value)
        public int ReadPointer(uint pointer)
        {
            //Create bytearray
            byte[] bytes = new byte[24];

            //Reading the specific address within the process
            ReadProcessMemory(Handle, (IntPtr)pointer, bytes, (UIntPtr)sizeof(int), 0);
            //Return the result as 4 byte int
            return BitConverter.ToInt32(bytes, 0);
        }

        public string HexIt(string yourString)
        {
            string hex = "";
            foreach (char c in yourString)
            {
                int tmp = c;
                hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
            }
            return hex;
        }

    }
}
